//
//
//
//
//
//
//
//
// DCUtil.cpp

#include "stdafx.h"
#include "DCUtil.h"


DCUtil::DCUtil():_state(DCUtil::RANG_INITIAL){}


DCUtil::DCUtil(HDC hdc) : _hdc(hdc), _state(DCUtil::RANG_INITIAL){};

DCUtil::DCUtil(HDC hdc, POINT start) : _hdc(hdc), _start(start), _state(DCUtil::RANG_INITIAL){};
DCUtil::DCUtil(HDC hdc, int x, int y) : _hdc(hdc), _state(DCUtil::RANG_INITIAL)
{
	_start.x = x;
	_start.y = y;
}

DCUtil::~DCUtil()
{};


void DCUtil::SetDC(const HDC& hdc)
{
	_hdc = hdc;
}

void DCUtil::CkStart(POINT start)
{
	_end = _start = start;
	_state = DCUtil::RANG_STARED;
}

void DCUtil::PointTo(POINT to)
{
	_end = to;
	_state = DCUtil::RANT_PROCESSING;
}

// Convert that start-end point to RECT structure
//
// a.....
// .    .
// .....b
//
//
void DCUtil::ToRect(RECT& rect)
{
	rect.left = _start.x < _end.x ? _start.x : _end.x;
	rect.right = _start.x > _end.x ? _start.x : _end.x;
	rect.top = _start.y < _end.y ? _start.y : _end.y;
	rect.bottom = _start.y > _end.y ? _start.y : _end.y;
}

void DCUtil::CkEnd(POINT end)
{
	_end = end;
	_state = DCUtil::RANG_ENDED;
}

POINT DCUtil::GetStartPoint(void)
{
	return _start;
}

POINT DCUtil::GetEndPoint(void)
{
	return _end;
}


void DCUtil::Clr(void)
{
	_start.x = _start.y = 0;
	_end.x = _end.y = 0;
	_state = DCUtil::RANG_INITIAL;
}


int DCUtil::GetWidth(void)
{
	if(_end.x > _start.x)
		return _end.x - _start.x ;
	return _start.x - _end.x ;
}
int DCUtil::GetHeight(void)
{
	if(_end.y > _start.y)
		return _end.y > _start.y;
	return _start.y - _end.y;
}

bool DCUtil::IsStart(void)
{
	return _state > DCUtil::RANG_INITIAL;
}


LONG& DCUtil::Left(void)
{
	return _start.x < _end.x ? _start.x : _end.x; 
}

LONG& DCUtil::Right(void)
{
	return _start.x > _end.x ? _start.x : _end.x; 
}

LONG& DCUtil::Top(void)
{
	return _start.y < _end.y ? _start.y :  _end.y;
}

LONG& DCUtil::Bottom(void)
{
	return  _start.y > _end.y ? _start.y :  _end.y;
}

DCUtil::RANG_STATE DCUtil::State(void)
{	
	return _state;
}

bool DCUtil::IsInRang(POINT point)
{
	if(point.x > Left() && point.x < Right()
		&& point.y < Bottom() && point.y > Top())
		return true;
	return false;
}


bool DCUtil::IsOnEdge(POINT point, EDGE_POS& cursorEdge)
{
	if(IsInRang(point))
	{
		cursorEdge = EDGE_POS_IN;
		return true;
	}
	
	if(point.x == Left())
	{
		if(point.y == Top())
			cursorEdge = EDGE_POS_LEFT_TOP;
		else if(point.y == Bottom())
			cursorEdge = EDGE_POS_LEFT_BOTTOM;
		else
			cursorEdge = EDGE_POS_LEFT;
		return true;
	}

	if(point.x == Right())
	{
		if(point.y == Top())
			cursorEdge = EDGE_POS_RIGHT_TOP;
		else if(point.y == Bottom())
			cursorEdge = EDGE_POS_RIGH_BOTTOM;
		else
			cursorEdge = EDGE_POS_RIGHT;
		return true;
	}

	if(point.y == Top())
	{
		cursorEdge = EDGE_POS_TOP;
		return true;
	}

	if(point.y == Bottom())
	{
		cursorEdge = EDGE_POS_BOTTOM;
		return true;
	}
	return false;		
}



void DCUtil::ReRangeDC(POINT a, POINT b, EDGE_POS edgeStatus)
{
	switch(edgeStatus)
	{
	case EDGE_POS_RIGHT:		
	case EDGE_POS_RIGH_BOTTOM:
	case EDGE_POS_RIGHT_TOP:
		Right() += (b.x - a.x);
		break;

	case EDGE_POS_LEFT:
	case EDGE_POS_LEFT_BOTTOM:
	case EDGE_POS_LEFT_TOP:
		Left() += (b.x - a.x);
		break;

	case EDGE_POS_BOTTOM:
		Bottom() += (b.y - a.y);		
		break;
	case EDGE_POS_TOP:		
		Top() += (b.y - a.y);
		break;
	}
}

